home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / Timing / EngClock96.lha / EngClock96 / Source / special.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-18  |  9.5 KB  |  445 lines

  1. /* Special "access" routines */
  2.  
  3. #include <clib/exec_protos.h>
  4. #include <clib/wb_protos.h>
  5. #include <clib/alib_protos.h>
  6. #include <clib/commodities_protos.h>
  7. #include <clib/intuition_protos.h>
  8. #include <clib/dos_protos.h>
  9. #include <libraries/commodities.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <intuition/intuition.h>
  14. #include <clib/intuition_protos.h>
  15. #include <exec/exec.h>
  16. #include <intuition/screens.h>
  17. #include <rexx/storage.h>
  18. #include <clib/rexxsyslib_protos.h>
  19. #include <libraries/arq.h>
  20.  
  21. /* #define DEBUG_CODE */
  22.  
  23. #define ENGLISH 1
  24. #define FRENCH 2
  25. #define SPANISH 3
  26.  
  27. struct Message *a_msg=NULL;
  28. struct MsgPort *CxMsgPort=NULL, *CxHotKey1=NULL, *CxHotKey2=NULL, *RexxPort=NULL;
  29. CxObj *CommObj, *hk1, *hk2;
  30. CxMsg *CxMessage;
  31.  
  32. extern struct Window *win_p;
  33. extern BOOL talk(char *text);
  34. extern char line2[200];
  35. extern char line1[200];
  36. extern struct Menu menu1;
  37. extern BOOL loadpic(char *fname, struct Screen *screen);
  38. extern void refreshwindow(void);
  39. extern char *trans(char *text);
  40. extern BOOL ispicture;
  41. extern int language;
  42. extern int prefsgui(void);
  43. extern void changemenu(void);
  44. extern BOOL exit_flag;
  45. extern void msg(char *msg);
  46. extern struct NewWindow win1;
  47. extern void closeall(void);
  48. extern void setupmainwindow(void);
  49.  
  50. extern struct Screen *pubscreen;
  51. extern struct Library *CxBase;
  52. extern struct IntuitionBase *IntuitionBase;
  53. extern struct GfxBase *GfxBase;
  54. extern struct Library *UtilityBase;
  55. extern struct Library *GadToolsBase;
  56. extern struct Library *DiskfontBase;
  57. extern struct Library *AslBase;
  58. extern struct Library *IFFParseBase;
  59. extern struct Library *DOSBase;
  60. extern struct Library *RexxSysBase;
  61.  
  62. BOOL cxinit(void);
  63. BOOL arexxinit(void);
  64. void cxclose(void);
  65. void arexxclose(void);
  66. void process_arexx(void);
  67.  
  68. struct Event {
  69.     BOOL used;
  70.     int type;    /* Type of event */
  71.     int hours;    /* Time of event */
  72.     int minutes;
  73.     int timecode;
  74.     char message[256];
  75.     int day;
  76.     int month;
  77.     int year;
  78.     BOOL enabledate;
  79.     int freq;
  80. };
  81.  
  82. extern struct {
  83. /* Prefs structure used for preferences file format */
  84.  
  85.    char header[10];  /* I.D. Header */
  86.    int vers;         /* Version of preferences file */
  87.    int x;            /* X dimension of window when saved */
  88.    int y;            /* Y dimension of window when saved */
  89.    int width;        /* Width of window when saved */
  90.    int height;       /* Height of window when saved */
  91.    int planguage;    /* Language selected (menu) when saved, low pri */
  92.    int just;         /* Justification of text, 0=Left, 1=Centre, 2=Right */
  93.    short date;       /* Display date?, 0 = No, 1=Yes */
  94.    short wtf;         /* Window to front ? */
  95.    int time_col[8];
  96.    int date_col[8];
  97.    short autoadjust;       /* Use automatic adjustment on window ? */
  98.    char backdrop[256];  /* Backdrop image */
  99.    char pub[139];            /* Default public screen */
  100.     char accent[256];        /* Language file for tRanslate */
  101.     char tkey[100];
  102.     char hkey[100];
  103.     struct Event events[11];  /* For the alarm! */
  104.  
  105. }prefs;
  106.  
  107. static struct NewBroker newbrok = {
  108.     NB_VERSION,
  109.     "EngClock 1",
  110.     "EngClock v6.6 © 1995 by Ben Matthew",
  111.     "A clock with words!",
  112.     NBU_UNIQUE|NBU_NOTIFY,
  113.     COF_SHOW_HIDE,
  114.     0,
  115.     0,
  116.     0
  117. };
  118.  
  119. BOOL cxinit(void) {
  120.     char comtext[50], tmp[10];
  121.     int count=1;
  122.     
  123.     #define MAXBROKERS 10
  124.  
  125.     if(!CxBase) 
  126.         return(0);
  127.  
  128.     CxMsgPort=CreateMsgPort();
  129.     CxHotKey1=CreateMsgPort();
  130.     CxHotKey2=CreateMsgPort();
  131.  
  132.     if(!(CxMsgPort && CxHotKey1 && CxHotKey2)) 
  133.         return(0);
  134.     
  135.     newbrok.nb_Port=CxMsgPort;
  136.     while(!(CommObj=CxBroker(&newbrok,NULL))) {
  137.         count++;
  138.         if(count==MAXBROKERS) break;
  139.         strcpy(comtext,"EngClock ");
  140.         sprintf(tmp,"%d",count);
  141.         strcat(comtext,tmp);
  142.         newbrok.nb_Name=comtext;
  143.     }
  144.     if(!CommObj) {
  145.         
  146.         DeleteMsgPort(CxMsgPort);
  147.         CxMsgPort=NULL;
  148.         return(0);
  149.     } else
  150.         ActivateCxObj(CommObj,1L);
  151.  
  152.     
  153.     hk1=HotKey(prefs.tkey,CxHotKey1,1234);
  154.     hk2=HotKey(prefs.hkey, CxHotKey2,9999);
  155.  
  156.     if(!(hk1 && hk2)) {
  157.         if(hk1) DeleteCxObj(hk1);
  158.         if(hk2) DeleteCxObj(hk2);
  159.         DeleteMsgPort(CxHotKey1);
  160.     } else {
  161.         AttachCxObj(CommObj,hk1);
  162.         AttachCxObj(CommObj,hk2);
  163.         ActivateCxObj(CommObj,1L);
  164.     }    
  165.     
  166.     return(1);
  167. }
  168.  
  169. void cxclose(void) {
  170.  
  171.     if(CommObj) 
  172.         DeleteCxObjAll(CommObj);
  173.  
  174.     
  175.     if(CxMsgPort) {
  176.         while(CxMessage=(CxMsg *)GetMsg(CxMsgPort)) 
  177.             ReplyMsg((struct Message *)CxMessage);
  178.         
  179.         DeleteMsgPort(CxMsgPort);
  180.         CxMsgPort=NULL;
  181.     }
  182.  
  183.     if(CxHotKey1) {
  184.         while(CxMessage=(CxMsg *)GetMsg(CxHotKey1)) 
  185.             ReplyMsg((struct Message *)CxMessage);
  186.         
  187.         DeleteMsgPort(CxHotKey1);
  188.         CxHotKey1=NULL;
  189.     }
  190.  
  191.     if(CxHotKey2) {
  192.         while(CxMessage=(CxMsg *)GetMsg(CxHotKey2)) 
  193.             ReplyMsg((struct Message *)CxMessage);
  194.         
  195.         DeleteMsgPort(CxHotKey2);
  196.         CxHotKey2=NULL;
  197.     }
  198.  
  199.     return;
  200. }
  201.  
  202. /* Now a simple routine that is used in main() to check what the user 
  203. wants to do about a menu selection */
  204.  
  205. BOOL wb_ask(char *body, char *opt1, char *opt2) {
  206.     struct ExtEasyStruct deadeasy;
  207.     char string[500];
  208.     char params[100];
  209.  
  210.     strcpy(string,"English Clock v6.9\n\n");
  211.     strcat(string,body);
  212.     strcpy(params,opt2);
  213.     strcat(params,"|");
  214.     strcat(params,opt1);
  215.  
  216.     deadeasy.Magic=ARQ_MAGIC;
  217.     deadeasy.AnimID=ARQ_ID_QUESTION;
  218.     deadeasy.Easy.es_Title=" ";
  219.     deadeasy.Easy.es_TextFormat=string;
  220.     deadeasy.Easy.es_GadgetFormat=params;
  221.  
  222.     return(EasyRequest(NULL,&deadeasy.Easy,NULL,NULL));
  223.  
  224. }
  225.  
  226. BOOL arexxinit(void) {
  227.     Forbid();
  228.     if(FindPort("ENG_REXX")) {
  229.         Permit();
  230.         return(0);
  231.     }
  232.     if(!(RexxPort=(struct MsgPort *)CreatePort("ENG_REXX",0))) {
  233.         Permit();
  234.         return(0);
  235.     }
  236.     Permit();
  237.     
  238.     // Best to set up RexxBase as well
  239.  
  240.     if(!(RexxSysBase=OpenLibrary("rexxsyslib.library",0))) {
  241.         DeletePort(RexxPort);
  242.         return(0);
  243.     }
  244.             
  245.     // And thats it!
  246.  
  247.     return(1);
  248.  
  249. }
  250.  
  251. void arexxclose(void) {
  252.     struct Message *a_mess=NULL;
  253.  
  254.     while(a_mess=GetMsg(RexxPort))
  255.         ReplyMsg(a_mess);
  256.     
  257.     Forbid();
  258.     DeletePort(RexxPort);
  259.     Permit();
  260.     RexxPort=NULL;
  261.  
  262.     if(RexxSysBase) CloseLibrary(RexxSysBase);
  263.     RexxSysBase=NULL;
  264. }
  265.  
  266. void process_arexx(void) {
  267.     struct RexxMsg *rexxmsg=NULL;
  268.     char string[50];
  269.     char newspeak[200];
  270.     char *ptr, *ptr2;
  271.     char string2[50];
  272.     BPTR file;
  273.     int x, y, width, height, count=7, gotted=0, len;
  274.  
  275.     while(rexxmsg=(struct RexxMsg *)GetMsg(RexxPort)) {
  276.  
  277.         // We have a message, but what is it!
  278.  
  279.         ptr=CreateArgstring(rexxmsg->rm_Args[0],strlen(rexxmsg->rm_Args[0]));
  280.         sprintf(string,"%s",ptr);
  281.  
  282.         if(!strcmp(string,"SPEAK")) {
  283.             strcpy(newspeak,line1);
  284.             if(language==ENGLISH) newspeak[strlen(newspeak)-4]=NULL;  /* Get rid of pm/am */
  285.             strcat(newspeak,".  ");
  286.             strcat(newspeak,line2);
  287.             newspeak[strlen(newspeak)-6]=NULL; /* Get rid of year */
  288.             talk(trans(newspeak));
  289.         }    
  290.         if(!strcmp(string,"PREFS")) {
  291.             prefsgui();
  292.             ClearMenuStrip(win_p);
  293.             changemenu();
  294.             SetMenuStrip(win_p,&menu1);
  295.             ispicture=loadpic(prefs.backdrop,pubscreen);
  296.             refreshwindow();
  297.         }
  298.         if(!strcmp(string,"ACTIVATE")) {
  299.             WindowToFront(win_p);
  300.         }
  301.         if(!strcmp(string,"QUIT")) {
  302.             exit_flag=TRUE;
  303.         }
  304.         if(!strcmp(string,"LEFT")) {
  305.             prefs.just=0;
  306.             refreshwindow();
  307.         }
  308.         if(!strcmp(string,"RIGHT")) {
  309.             prefs.just=2;
  310.             refreshwindow();
  311.         }
  312.         if(!strcmp(string,"CENTRE")) {
  313.             prefs.just=1;
  314.             refreshwindow();
  315.         }
  316.         if(!strcmp(string,"ENGLISH")) {
  317.             language=ENGLISH;
  318.             refreshwindow();
  319.             ClearMenuStrip(win_p);
  320.             changemenu();
  321.             SetMenuStrip(win_p,&menu1);
  322.         }
  323.         if(!strcmp(string,"FRENCH")) {
  324.             language=FRENCH;
  325.             refreshwindow();
  326.             ClearMenuStrip(win_p);
  327.             changemenu();
  328.             SetMenuStrip(win_p,&menu1);
  329.         }
  330.         if(!strcmp(string,"SPANISH")) {
  331.             language=SPANISH;
  332.             refreshwindow();
  333.             ClearMenuStrip(win_p);
  334.             changemenu();
  335.             SetMenuStrip(win_p,&menu1);
  336.         }
  337.         if(!strcmp(string,"AUTO ON")) {
  338.             prefs.autoadjust=1;
  339.             refreshwindow();            
  340.         }
  341.         if(!strcmp(string,"AUTO OFF")) {
  342.             prefs.autoadjust=0;
  343.             refreshwindow();            
  344.         }
  345.         if(!strcmp(string,"DATE ON")) {
  346.             prefs.date=1;
  347.             refreshwindow();            
  348.         }
  349.         if(!strcmp(string,"DATE OFF")) {
  350.             prefs.autoadjust=0;
  351.             refreshwindow();            
  352.         }
  353.         strcpy(string2,string);
  354.         string2[8]=NULL;
  355.         if(!strcmp(string2,"SENDPORT")) {
  356.             ptr2=&string2[9];
  357.             if(file=Open(ptr2,MODE_OLDFILE)) {
  358.                 Write(file,line1,strlen(line1));
  359.                 Close(file);
  360.             }
  361.         }
  362.         if(!strcmp(string2,"SENDFILE")) {
  363.             ptr2=&string2[9];
  364.             if(file=Open(ptr2,MODE_READWRITE)) {
  365.                 Write(file,line1,strlen(line1));
  366.                 Close(file);
  367.             }
  368.         }
  369.         strcpy(string2,string);
  370.         string2[6]=NULL;
  371.         if(!strcmp(string2,"SCREEN")) {
  372.             ptr2=&string2[7];
  373.             sprintf(prefs.pub,"%s",ptr2);
  374.         }
  375.         if(!strcmp(string2,"ACCENT")) {
  376.             ptr2=&string2[7];
  377.             sprintf(prefs.accent,"%s",ptr2);
  378.         }
  379.         strcpy(string2,string);
  380.         string2[5]=NULL;
  381.         if(!strcmp(string2,"PICCY")) {
  382.             ptr2=&string2[6];
  383.             sprintf(prefs.backdrop,ptr2);
  384.             ispicture=loadpic(prefs.backdrop,pubscreen);
  385.             refreshwindow();
  386.         }
  387.         strcpy(string2,string);
  388.         string2[6]=NULL;
  389.         count=7;
  390.         if(!strcmp(string2,"NEWWIN")) {
  391.             strcpy(string2,string); /* Re-init string2 */
  392.             ptr2=&string2[7];
  393.             len=strlen(string2);
  394.             while(count <= len) {
  395.                 if(string[count]==NULL)  /* End of line yet? */
  396.                     height=atoi(ptr2);
  397.                 
  398.  
  399.                 if(string2[count]==',') {
  400.                     string2[count]=NULL;
  401.  
  402.                     switch(gotted) {
  403.                         case 0:
  404.                             x=atoi(ptr2);
  405.                         break;
  406.                         case 1:
  407.                             y=atoi(ptr2);
  408.                         break;
  409.                         case 2:
  410.                             width=atoi(ptr2);
  411.                         break;
  412.                     }
  413.                     gotted++;
  414.                     ptr2=&string2[count]+1;
  415.                 }    
  416.                 
  417.                 count++;
  418.             }
  419.             if(gotted==3) {
  420.                 // If not there has been a parse error
  421.                 prefs.x=x;
  422.                 prefs.y=y;
  423.                 prefs.width=width;
  424.                 prefs.height=height;
  425.                 ClearMenuStrip(win_p);
  426.                 CloseWindow(win_p);
  427.                 setupmainwindow();
  428.                 if(!(win_p=OpenWindow(&win1))) {
  429.                     msg("Error re-opening window!");
  430.                     // Best to reply before bye
  431.                     ReplyMsg((struct Message *)rexxmsg);
  432.                     closeall();
  433.                     exit(NULL);
  434.                 }
  435.                 SetMenuStrip(win_p,&menu1);
  436.                 refreshwindow();
  437.             }
  438.         }
  439.  
  440.  
  441.         ReplyMsg((struct Message *)rexxmsg);
  442.     }
  443.     DeleteArgstring(ptr);
  444. }    
  445.